template <class T>
inline T *MediaManager::getMedia(const char *filename)
{
    T *media = NULL;
    try
    {
        ILoader<T> *loader = findLoader<T>(filename);
        media = loader->loadFromFile(filename);
    }
    catch(const std::exception& e)
    {
        Logger::log() << e.what() << "\n";
        media = NULL;
    }
    return static_cast<T*>(media);
}

template <class T>
inline void MediaManager::saveMedia(const T* object, const char *filename)
{
	try
	{
		ILoader<T> *loader = findLoader<T>(filename);
		loader->saveToFile(object, filename);
	}
	catch(std::exception& e)
	{
		Logger::log() << e.what() << "\n";
	}
}

template <class T>
inline ILoader<T> *MediaManager::findLoader(const char *filename)
{
    std::string extension = getExtension(filename);

    typename MediaHolder<T>::TLoadersMap::const_iterator it = MediaHolder<T>::_loaders.find(toLower(extension));

    if((it != MediaHolder<T>::_loaders.end()) && it->second)
        return it->second;

    throw LoadingFailed(filename, "No loaders for this extension.");
}

template <class T>
inline void MediaManager::registerLoader(ILoader<T> *loader, const std::string &extensions)
{
    std::vector<std::string> ext;
    split(extensions, ext, " /\\*.,;|-_\t\n'\"");

    for(std::vector<std::string>::iterator it = ext.begin(); it != ext.end(); ++it)
        MediaHolder<T>::_loaders[toLower(*it)] = loader;
}

template <class T>
inline void MediaManager::unregisterLoader()
{
    typename MediaHolder<T>::TLoadersMap::const_iterator it = MediaHolder<T>::_loaders.begin();
    if(it->second) delete it->second;
    MediaHolder<T>::_loaders.clear();
}
